package leetcode.code1235;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
		int len = startTime.length;
		Integer[] ends = new Integer[len];
		for (int i = 0; i < ends.length; i++) {
			ends[i] = i;
		}
		Arrays.sort(ends, (a, b) -> endTime[a] - endTime[b]);
		TreeMap<Integer, Integer> tm = new TreeMap<>();
		int max = 0;
		for (int i = 0, lastTime = -1; i < ends.length; i++) {
			int index = ends[i], start = startTime[index], end = endTime[index], pro = profit[index];
			if (end != lastTime) {
				tm.put(lastTime, max);
				lastTime = end;
			}
			Map.Entry<Integer, Integer> entry = tm.floorEntry(start);
			max = Math.max(max, entry.getValue() + pro);
		}
		return max;
	}

	@Override
	public void debug1() {
		H.compare(150, this.jobScheduling(H.str2array3("[1,2,3,4,6]"), H.str2array3("[3,5,10,6,9]"), H.str2array3("[20,20,100,70,60]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();

	}

}
